Vamshiboss8055 commited on
Commit
ddeb907
·
verified ·
1 Parent(s): 4df4505

Upload 2 files

Browse files
Files changed (2) hide show
  1. gradio_app.py +73 -0
  2. requirements.txt +2 -0
gradio_app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import logging
4
+
5
+ logger = logging.getLogger(__name__)
6
+ logging.basicConfig(level=logging.INFO)
7
+
8
+ # OpenRouter API Configuration
9
+ API_KEY = "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"
10
+ BASE_URL = "https://openrouter.ai/api/v1/chat/completions"
11
+ MODEL = "arcee-ai/trinity-large-preview:free"
12
+
13
+ def get_ai_response(user_message, history):
14
+ """Get response from OpenRouter API"""
15
+ try:
16
+ headers = {
17
+ "Authorization": f"Bearer {API_KEY}",
18
+ "Content-Type": "application/json"
19
+ }
20
+
21
+ # Build messages from history
22
+ messages = [{"role": "system", "content": "You are a helpful chatbot. Provide concise and helpful responses."}]
23
+
24
+ # Add history - handle different formats safely
25
+ if history:
26
+ for item in history:
27
+ if isinstance(item, (list, tuple)) and len(item) >= 2:
28
+ messages.append({"role": "user", "content": item[0]})
29
+ if item[1]: # Only add if assistant response exists
30
+ messages.append({"role": "assistant", "content": item[1]})
31
+
32
+ messages.append({"role": "user", "content": user_message})
33
+
34
+ payload = {
35
+ "model": MODEL,
36
+ "messages": messages,
37
+ "temperature": 0.7,
38
+ "max_tokens": 500
39
+ }
40
+
41
+ response = requests.post(BASE_URL, headers=headers, json=payload, timeout=10)
42
+ response.raise_for_status()
43
+
44
+ data = response.json()
45
+ if "choices" in data and len(data["choices"]) > 0:
46
+ return data["choices"][0]["message"]["content"]
47
+ else:
48
+ return "I apologize, but I couldn't generate a response. Please try again."
49
+
50
+ except Exception as e:
51
+ logger.error(f"API Error: {str(e)}")
52
+ return f"Error: {str(e)}"
53
+
54
+ def chat(user_message, history):
55
+ """Main chat function"""
56
+ if not user_message.strip():
57
+ return ""
58
+
59
+ # Get AI response
60
+ bot_response = get_ai_response(user_message, history)
61
+
62
+ return bot_response
63
+
64
+ # Create the Gradio interface
65
+ demo = gr.ChatInterface(
66
+ chat,
67
+ examples=["Hello"],
68
+ title="SAK Informatics Chatbot By Namani Vamshi Krishna",
69
+ description="Chat with an AI assistant",
70
+ )
71
+
72
+ if __name__ == "__main__":
73
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio==4.26.0
2
+ requests==2.31.0