Danielbrdz commited on
Commit
be9f0b4
·
verified ·
1 Parent(s): 5f2dab6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+
49
+ accumulated_response = ""
50
+ for line in response.iter_lines():
51
+ if line:
52
+
53
+ line_text = line.decode('utf-8')
54
+ if line_text.startswith("data: "):
55
+ data_str = line_text[6:]
56
+
57
+
58
+ if data_str == "[DONE]":
59
+ break
60
+
61
+ try:
62
+ data = json.loads(data_str)
63
+ if 'choices' in data and len(data['choices']) > 0:
64
+ delta = data['choices'][0].get('delta', {})
65
+ if 'content' in delta and delta['content']:
66
+ token = delta['content']
67
+ accumulated_response += token
68
+ yield accumulated_response
69
+ except json.JSONDecodeError:
70
+ continue
71
+
72
+ if not accumulated_response:
73
+ yield "Lo siento, ocurrió un error al procesar tu solicitud."
74
+
75
+ demo = gr.ChatInterface(
76
+ respond,
77
+ examples=[["¡Bienvenido a Barcenas Gran Bajio!"],
78
+ ["¿Cuál es el objetivo principal de El Gran Bajío Network?"],
79
+ ["¿Qué beneficios clave obtengo al ser miembro?"],
80
+ ["¿Qué tipo de actividades o conexiones fomenta la red?"]
81
+ ]
82
+ )
83
+
84
+ if __name__ == "__main__":
85
+ demo.launch()