Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import requests
|
5 |
+
|
6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
7 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
8 |
+
|
9 |
+
SYSTEM_MESSAGE = os.environ.get("System_Prompt")
|
10 |
+
MODEL_NAME = "meta-llama/llama-4-maverick-17b-128e-instruct"
|
11 |
+
MAX_TOKENS = 1024
|
12 |
+
TEMPERATURE = 0.7
|
13 |
+
TOP_P = 0.95
|
14 |
+
|
15 |
+
def respond(message, history: list[tuple[str, str]]):
|
16 |
+
# Preparar los mensajes en el formato que Groq espera
|
17 |
+
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
18 |
+
|
19 |
+
for user_msg, assistant_msg in history:
|
20 |
+
if user_msg:
|
21 |
+
messages.append({"role": "user", "content": user_msg})
|
22 |
+
if assistant_msg:
|
23 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
24 |
+
|
25 |
+
messages.append({"role": "user", "content": message})
|
26 |
+
|
27 |
+
headers = {
|
28 |
+
"Content-Type": "application/json",
|
29 |
+
"Authorization": f"Bearer {GROQ_API_KEY}"
|
30 |
+
}
|
31 |
+
|
32 |
+
payload = {
|
33 |
+
"model": MODEL_NAME,
|
34 |
+
"messages": messages,
|
35 |
+
"max_tokens": MAX_TOKENS,
|
36 |
+
"temperature": TEMPERATURE,
|
37 |
+
"top_p": TOP_P,
|
38 |
+
"stream": True
|
39 |
+
}
|
40 |
+
|
41 |
+
response = requests.post(
|
42 |
+
GROQ_API_URL,
|
43 |
+
headers=headers,
|
44 |
+
json=payload,
|
45 |
+
stream=True
|
46 |
+
)
|
47 |
+
|
48 |
+
accumulated_response = ""
|
49 |
+
for line in response.iter_lines():
|
50 |
+
if line:
|
51 |
+
|
52 |
+
line_text = line.decode('utf-8')
|
53 |
+
if line_text.startswith("data: "):
|
54 |
+
data_str = line_text[6:]
|
55 |
+
|
56 |
+
|
57 |
+
if data_str == "[DONE]":
|
58 |
+
break
|
59 |
+
|
60 |
+
try:
|
61 |
+
data = json.loads(data_str)
|
62 |
+
if 'choices' in data and len(data['choices']) > 0:
|
63 |
+
delta = data['choices'][0].get('delta', {})
|
64 |
+
if 'content' in delta and delta['content']:
|
65 |
+
token = delta['content']
|
66 |
+
accumulated_response += token
|
67 |
+
yield accumulated_response
|
68 |
+
except json.JSONDecodeError:
|
69 |
+
continue
|
70 |
+
|
71 |
+
if not accumulated_response:
|
72 |
+
yield "Lo siento, ocurri贸 un error al procesar tu solicitud."
|
73 |
+
|
74 |
+
|
75 |
+
demo = gr.ChatInterface(
|
76 |
+
respond,
|
77 |
+
examples=[["隆Bienvenido a Barcenas PostGBN! Escribe tus instrucciones y ahorra tiempo"]]
|
78 |
+
)
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
demo.launch()
|