vinzur commited on
Commit
5f5d50c
·
1 Parent(s): 06fa5ea

Add prompt and llm code

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import os
4
+ from mistralai import Mistral, UserMessage, SystemMessage
5
+
6
+ # Get the API key and endpoint
7
+ token = os.environ.get("GITHUB_TOKEN")
8
+ endpoint = "https://models.github.ai/inference"
9
+ model_name = "mistral-ai/Codestral-2501"
10
+
11
+ # Initialize the Mistral client
12
+ client = Mistral(api_key=token, server_url=endpoint)
13
+
14
+ def generate_response(query, diagram_type):
15
+ if diagram_type == "ERD":
16
+ system_prompt = (
17
+ "You are a code generation expert. Convert the following PlantUML code into "
18
+ "equivalent SQL code. Give only code as output."
19
+ )
20
+ elif diagram_type == "Use Case Diagram":
21
+ system_prompt = (
22
+ "You are a code generation expert. Convert the following PlantUML code into "
23
+ "equivalent Java REST API controller methods."
24
+ )
25
+ elif diagram_type in ["Class Diagram", "Sequence Diagram"]:
26
+ system_prompt = (
27
+ "You are a code generation expert. Convert the following PlantUML code into "
28
+ "equivalent Java code. Give only code as output."
29
+ )
30
+ else:
31
+ system_prompt = "You are a helpful coding assistant."
32
+
33
+ user_prompt = query.strip()
34
+
35
+ response = client.chat.complete(
36
+ model=model_name,
37
+ messages=[
38
+ SystemMessage(system_prompt),
39
+ UserMessage(user_prompt),
40
+ ],
41
+ max_tokens=1000,
42
+ model_name=model_name,
43
+ )
44
+
45
+ return response.choices[0].message.content
46
+
47
+ # ---------------- Gradio Interface ----------------
48
+
49
+ @spaces.GPU
50
+ def predict(query, diagram_type):
51
+ return generate_response(query, diagram_type)
52
+
53
+ iface = gr.Interface(
54
+ fn=predict,
55
+ inputs=[
56
+ gr.Textbox(lines=15, label="PlantUML Code"),
57
+ gr.Dropdown(
58
+ choices=["ERD", "Use Case Diagram", "Class Diagram", "Sequence Diagram"],
59
+ label="Diagram Type"
60
+ )
61
+ ],
62
+ outputs="text",
63
+ title="PlantUML-To-Code Converter",
64
+ description="Enter PlantUML code to generate equivalent SQL or Java code based on the diagram type."
65
+ )
66
+
67
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio>=4.0.0
2
+ spaces>=0.16.0
3
+ mistralai>=1.0.0