Anupam251272 commited on
Commit
b6fb366
Β·
verified Β·
1 Parent(s): 50c0887

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import os
4
+
5
+ # Initialize the Groq client
6
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
7
+
8
+ # Define the chat function
9
+ def chat_with_tendulkar(message, history):
10
+ prompt = f"""You are a chatbot impersonating Sachin Tendulkar, the legendary cricketer.
11
+ You have extensive knowledge about cricket, including its history, rules, tournaments, and players.
12
+ Respond to the following message as Sachin Tendulkar would. After your main response, always include a new line with "Did you know! πŸ˜‚" followed by a short, fun fact related to cricket. The fun fact should be no more than 15 words.
13
+
14
+ Human: {message}
15
+ Sachin Tendulkar:"""
16
+
17
+ full_history = "\n".join([f"Human: {h[0]}\nSachin Tendulkar: {h[1]}" for h in history])
18
+ full_prompt = full_history + "\n" + prompt if full_history else prompt
19
+
20
+ chat_completion = client.chat.completions.create(
21
+ messages=[
22
+ {
23
+ "role": "user",
24
+ "content": full_prompt,
25
+ }
26
+ ],
27
+ model="mixtral-8x7b-32768",
28
+ temperature=0.7,
29
+ max_tokens=1000,
30
+ top_p=1,
31
+ stream=False,
32
+ )
33
+
34
+ response = chat_completion.choices[0].message.content
35
+
36
+ if "Did you know!" in response and not response.endswith("\n"):
37
+ response = response.replace("Did you know!", "\nDid you know! πŸ˜‚")
38
+ elif "Did you know!" in response:
39
+ response = response.replace("Did you know!", "Did you know! πŸ˜‚")
40
+
41
+ return response
42
+
43
+ # Custom CSS for center alignment
44
+ custom_css = """
45
+ .center-text {
46
+ text-align: center;
47
+ margin: auto;
48
+ max-width: 90%;
49
+ }
50
+ """
51
+
52
+ # Create the Gradio interface with custom CSS and updated title
53
+ with gr.Blocks(css=custom_css) as iface:
54
+ gr.Markdown(
55
+ "# 🏏 Chat with the Master Blaster, Sachin Tendulkar πŸ†",
56
+ elem_classes=["center-text"]
57
+ )
58
+ gr.Markdown(
59
+ "🌟 Serve up your questions to the cricket legend! 🏏 Get expert insights on cricket, techniques, and more. Let's hit some knowledge! πŸ’¬πŸ†",
60
+ elem_classes=["center-text"]
61
+ )
62
+ chatbot = gr.ChatInterface(
63
+ chat_with_tendulkar,
64
+ examples=[
65
+ "What's your favorite cricket tournament and why?",
66
+ "Can you explain the difference between ODI and Test cricket?",
67
+ "What do you think about the current state of Indian cricket?",
68
+ "Any tips for improving my batting skills?",
69
+ "What was your most memorable match and why?",
70
+ ],
71
+ )
72
+
73
+ # Launch the interface
74
+ iface.launch()