acecalisto3 commited on
Commit
71d2bac
·
verified ·
1 Parent(s): 71d1038

Update apps.py

Browse files
Files changed (1) hide show
  1. apps.py +189 -64
apps.py CHANGED
@@ -1,65 +1,190 @@
1
- import streamlit as st
2
- from smolagents.agents import ToolCallingAgent
3
- from smolagents import tool, LiteLLMModel
4
- from typing import Optional
5
- import cv2
6
- import pytesseract
7
- from PIL import Image
8
- import io
9
- import numpy as np
10
- import base64
11
-
12
- # Define the LiteLLMModel with OpenAI key
13
- model = LiteLLMModel(model_id="gpt-4o", api_key="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")
14
-
15
- @tool
16
- def extract_components(image_data_base64: str) -> str:
17
- """
18
- Extract components from a web design image.
19
-
20
- Args:
21
- image_data_base64: The image data in base64 string format.
22
-
23
- Returns:
24
- A string describing the components found in the image.
25
- """
26
- image_data = base64.b64decode(image_data_base64)
27
- image = Image.open(io.BytesIO(image_data))
28
- gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
29
- components = pytesseract.image_to_string(gray)
30
- return components
31
-
32
- @tool
33
- def generate_code(components: str) -> str:
34
- """
35
- Generate code for the given components.
36
-
37
- Args:
38
- components: A string describing the components.
39
-
40
- Returns:
41
- The generated code for the components.
42
- """
43
- # This is a placeholder implementation. You can replace it with actual code generation logic.
44
- return f"Generated code for components: {components}"
45
-
46
- # Define the ToolCallingAgent
47
- agent = ToolCallingAgent(tools=[extract_components, generate_code], model=model)
48
-
49
- # Streamlit app title
50
- st.title("Web Design Component Extractor")
51
-
52
- # File uploader for the web design image
53
- uploaded_file = st.file_uploader("Upload a web design image", type=["png", "jpg", "jpeg"])
54
-
55
- # Button to run the agent
56
- if st.button("Extract and Generate Code"):
57
- if uploaded_file is not None:
58
- image_data = uploaded_file.read()
59
- image_data_base64 = base64.b64encode(image_data).decode('utf-8')
60
- components = agent.run(f"extract_components {image_data_base64}")
61
- code = agent.run(f"generate_code {components}")
62
- st.write("Extracted Components:", components)
63
- st.write("Generated Code:", code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  else:
65
- st.write("Please upload an image.")
 
1
+ async def respond(
2
+ command: str,
3
+ history: List[Tuple[str, str]],
4
+ system_message: str,
5
+ max_tokens: int,
6
+ temperature: float,
7
+ top_p: float,
8
+ github_api_token: str,
9
+ github_username: str,
10
+ github_repository: str,
11
+ selected_model: str,
12
+ severity: str,
13
+ programming_language: str,
14
+ *args
15
+ ) -> str:
16
+ global GITHUB_API_TOKEN
17
+ GITHUB_API_TOKEN = github_api_token
18
+ global issues
19
+ issues = []
20
+ global github_client
21
+ github_client = None
22
+
23
+ messages = [{"role": "system", "content": system_message}]
24
+ logging.info("System message: {}".format(system_message))
25
+
26
+ for user_msg, assistant_msg in history:
27
+ if user_msg:
28
+ messages.append({"role": "user", "content": user_msg})
29
+ logging.info("User message: {}".format(user_msg))
30
+ if assistant_msg:
31
+ messages.append({"role": "assistant", "content": assistant_msg})
32
+ logging.info("Assistant message: {}".format(assistant_msg))
33
+
34
+ logging.info("Command received: {}".format(command))
35
+
36
+ try:
37
+ command, *args = command.split(' ', 1)
38
+ args = args[0] if args else ''
39
+ except ValueError:
40
+ yield "❌ Invalid command format. Use /help for instructions"
41
+
42
+ if command == "/github":
43
+ try:
44
+ if not args:
45
+ if github_client:
46
+ yield f"ℹ️ Current GitHub connection: {github_client.config.username}/{github_client.config.repository}"
47
+ else:
48
+ yield "ℹ️ Not connected to GitHub"
49
+
50
+ parts = args.split(maxsplit=2) # Allow spaces in token
51
+ if len(parts) < 3:
52
+ raise ValueError("Format: /github <username> <repo> <token>")
53
+
54
+ github_client = GitHubIntegration(GitHubConfig(
55
+ username=parts[0],
56
+ repository=parts[1],
57
+ api_token=SecretStr(parts[2])
58
+ ))
59
+ issues = await github_client.fetch_issues() # Fetch issues after successful connection
60
+ yield " GitHub configured successfully"
61
+ except Exception as e:
62
+ github_client = None
63
+ yield f" Error: {str(e)}"
64
+
65
+ elif command == "/help":
66
+ help_message = """Available commands:
67
+ - `/github <username> <repo> <token>`: Connect to a GitHub repository.
68
+ - `/help`: Show this help message.
69
+ - `/generate_code [code description]`: Generate code based on the description.
70
+ - `/explain_concept [concept]`: Explain a concept.
71
+ - `/write_documentation [topic]`: Write documentation for a given topic.
72
+ - `/translate_code [code] to [target language]`: Translate code to another language.
73
+ - `/analyze [issue number]`: Analyze a GitHub issue.
74
+ - `/list_issues`: List all issues in the connected repository.
75
+ """
76
+ yield help_message
77
+
78
+ elif command.isdigit() and issues:
79
+ try:
80
+ issue_number = int(command) - 1
81
+ issue = issues[issue_number]
82
+ issue_text = issue['title'] + "\n\n" + issue['body']
83
+ resolution = analyze_issues(issue_text, selected_model, severity, programming_language)
84
+
85
+ related_issues = find_related_issues(issue_text, issues)
86
+ related_issue_text = "\n".join(
87
+ ["- {} (Similarity: {:.2f})".format(issue['title'], similarity) for issue, similarity in related_issues]
88
+ )
89
+
90
+ yield "Resolution for Issue '{}':\n{}\n\nRelated Issues:\n{}".format(issue['title'], resolution, related_issue_text)
91
+ except Exception as e:
92
+ logging.error("Error analyzing issue: {}".format(e))
93
+ yield "Error analyzing issue: {}".format(e)
94
+
95
+ elif command.startswith("/generate_code"):
96
+ code_description = command.replace("/generate_code", "").strip()
97
+ if not code_description:
98
+ yield "Please provide a description of the code you want to generate."
99
+ else:
100
+ prompt = "Generate code for the following: {}\nProgramming Language: {}".format(code_description, programming_language)
101
+ try:
102
+ generated_code = analyze_issues(prompt, selected_model)
103
+ code_output = "<pre>{}</pre>".format(generated_code)
104
+ yield code_output
105
+ except Exception as e:
106
+ logging.error("Error generating code: {}".format(e))
107
+ yield "Error generating code: {}".format(e)
108
+
109
+ elif command.startswith("/explain_concept"):
110
+ concept = command.replace("/explain_concept", "").strip()
111
+ if not concept:
112
+ yield "Please provide a concept to explain."
113
+ else:
114
+ prompt = "Explain the concept of {} in detail.".format(concept)
115
+ try:
116
+ explanation = analyze_issues(prompt, selected_model)
117
+ yield "<pre>{}</pre>".format(explanation)
118
+ except Exception as e:
119
+ logging.error("Error explaining concept: {}".format(e))
120
+ yield "Error explaining concept: {}".format(e)
121
+
122
+ elif command.startswith("/write_documentation"):
123
+ topic = command.replace("/write_documentation", "").strip()
124
+ if not topic:
125
+ yield "Please provide a topic for documentation."
126
+ else:
127
+ prompt = "Write documentation for the topic: {}".format(topic)
128
+ try:
129
+ documentation = analyze_issues(prompt, selected_model)
130
+ yield "<pre>{}</pre>".format(documentation)
131
+ except Exception as e:
132
+ logging.error("Error writing documentation: {}".format(e))
133
+ yield "Error writing documentation: {}".format(e)
134
+
135
+ elif command.startswith("/translate_code"):
136
+ try:
137
+ code, _, target_language = command.replace("/translate_code", "").strip().partition(" to ")
138
+ if not code or not target_language:
139
+ yield "Please provide code and target language in the format: `/translate_code [code] to [target language]`"
140
+ else:
141
+ prompt = f"Translate the following code to {target_language}:\n```\n{code}\n```"
142
+ try:
143
+ translated_code = analyze_issues(prompt, selected_model)
144
+ yield "<pre>{}</pre>".format(translated_code)
145
+ except Exception as e:
146
+ logging.error("Error translating code: {}".format(e))
147
+ yield "Error translating code: {}".format(e)
148
+ except Exception as e:
149
+ logging.error("Error parsing translate_code command: {}".format(e))
150
+ yield "Error parsing translate_code command: {}".format(e)
151
+
152
+ elif command.startswith("/analyze"):
153
+ try:
154
+ if not github_client:
155
+ yield "❌ You need to connect to a GitHub repository first using `/github <username> <repo> <token>`."
156
+ issue_number = int(command.replace("/analyze", "").strip()) - 1
157
+ if 0 <= issue_number < len(issues):
158
+ issue = issues[issue_number]
159
+ issue_text = issue['title'] + "\n\n" + issue['body']
160
+ resolution = analyze_issues(issue_text, selected_model, severity, programming_language)
161
+
162
+ related_issues = find_related_issues(issue_text, issues)
163
+ related_issue_text = "\n".join(
164
+ ["- {} (Similarity: {:.2f})".format(issue['title'], similarity) for issue, similarity in related_issues]
165
+ )
166
+
167
+ yield "Resolution for Issue '{}':\n{}\n\nRelated Issues:\n{}".format(issue['title'], resolution, related_issue_text)
168
+ else:
169
+ yield "❌ Invalid issue number. Please enter a valid issue number from the list."
170
+ except Exception as e:
171
+ logging.error("Error analyzing issue: {}".format(e))
172
+ yield "Error analyzing issue: {}".format(e)
173
+
174
+ elif command == "/list_issues":
175
+ try:
176
+ if not github_client:
177
+ yield "❌ You need to connect to a GitHub repository first using `/github <username> <repo> <token>`."
178
+ if issues:
179
+ issue_list = "\n".join(
180
+ [f"- {issue['title']} (Issue #{issue['number']})" for issue in issues]
181
+ )
182
+ yield f"Issues in {github_client.config.username}/{github_client.config.repository}:\n{issue_list}"
183
+ else:
184
+ yield "❌ No issues found in the connected repository."
185
+ except Exception as e:
186
+ logging.error("Error listing issues: {}".format(e))
187
+ yield "Error listing issues: {}".format(e)
188
+
189
  else:
190
+ yield "I'm not sure what you mean. Try using `/help` for a list of available commands."