AThirumoorthi commited on
Commit
f115f67
·
verified ·
1 Parent(s): d663b9d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+ import gradio as gr
4
+ import logging
5
+
6
+ os.environ["GROQ_API_KEY"] = "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"
7
+
8
+ logging.basicConfig(level=logging.DEBUG)
9
+ logger = logging.getLogger(__name__)
10
+
11
+ # Initialize the Groq client
12
+ api_key = os.environ.get("GROQ_API_KEY")
13
+ if not api_key:
14
+ logger.error("GROQ_API_KEY environment variable is not set.")
15
+ raise ValueError("GROQ_API_KEY environment variable is required.")
16
+ client = Groq(api_key=api_key)
17
+
18
+ MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
19
+
20
+ # Define a function to handle chat completions
21
+ def get_completion(user_input):
22
+ if not user_input.strip():
23
+ return "Please enter a valid query."
24
+
25
+ # Check if the user asks "Who made you?"
26
+ if "who made you" in user_input.lower():
27
+ return "I was created by Thirumoorthi, a brilliant mind working on AI systems!"
28
+
29
+ try:
30
+ completion = client.chat.completions.create(
31
+ model=MODEL_NAME,
32
+ messages=[
33
+ {"role": "system", "content": "You are a friendly and helpful assistant, like ChatGPT."},
34
+ {"role": "user", "content": user_input}
35
+ ],
36
+ temperature=0.7, # Slightly lower temperature for more controlled responses
37
+ max_tokens=1024,
38
+ top_p=1,
39
+ stream=True,
40
+ stop=None,
41
+ )
42
+
43
+ response = ""
44
+ for chunk in completion:
45
+ response += chunk.choices[0].delta.content or ""
46
+
47
+ return response.strip() # Clean up response
48
+ except Exception as e:
49
+ logger.error(f"Error during completion: {e}")
50
+ return "Sorry, I encountered an error while processing your request."
51
+
52
+ # Launch Gradio interface
53
+ def launch_interface():
54
+ demo = gr.Interface(
55
+ fn=get_completion,
56
+ inputs=gr.Textbox(
57
+ label="Ask me anything:",
58
+ placeholder="I am here to help! Ask away...",
59
+ lines=2,
60
+ max_lines=5,
61
+ show_label=True,
62
+ interactive=True
63
+ ),
64
+ outputs=gr.Textbox(
65
+ label="Response:",
66
+ interactive=False,
67
+ show_label=True,
68
+ lines=6,
69
+ max_lines=10
70
+ ),
71
+ title="Chat with Mr AI",
72
+ description="I am your friendly assistant, just like ChatGPT! Ask me anything, and I will do my best to help.",
73
+ theme="huggingface", # More modern theme
74
+ css="""
75
+ .gr-box { border-radius: 15px; border: 1px solid #e1e1e1; padding: 20px; background-color: #f9f9f9; }
76
+ .gr-button { background-color: #4CAF50; color: white; font-size: 14px; }
77
+ .gr-textbox { border-radius: 8px; font-size: 16px; padding: 10px; }
78
+ .gr-output { background-color: #f1f1f1; border-radius: 8px; font-size: 16px; padding: 15px; }
79
+ """,
80
+ allow_flagging="never",
81
+ live=True, # Enable live updates if supported
82
+ )
83
+
84
+ logger.info("Starting Gradio interface")
85
+ demo.launch(share=True)
86
+
87
+ if __name__ == "__main__":
88
+ launch_interface()