Habib U Rehman commited on
Commit
eb3fe64
·
verified ·
1 Parent(s): 3617789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -1,30 +1,23 @@
 
1
  import gradio as gr
2
  import os
3
  from groq import Groq
4
 
5
- # Initialize Groq client using your API key from environment
6
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
7
 
8
- # System prompt that instructs the AI to behave like a history expert
9
  SYSTEM_PROMPT = """
10
  You are a professional history expert AI assistant.
11
 
12
- When the user provides a country or region and a year or time period,
13
- identify the historically accurate ruling authority.
14
-
15
- Always include:
16
  - Ruler / Authority Name
17
  - Period of Rule
18
  - Region / Country
19
  - Key Contributions (bullet points)
20
-
21
- Be factually accurate and student-friendly.
22
  """
23
 
24
- # Function to send request to Groq AI
25
  def history_ruler(country, year):
26
  if not country or not year:
27
- return "Please enter both country/region and year."
28
 
29
  response = client.chat.completions.create(
30
  model="llama-3.1-8b-instant",
@@ -34,16 +27,34 @@ def history_ruler(country, year):
34
  ],
35
  temperature=0.2
36
  )
37
-
38
  return response.choices[0].message.content
39
 
40
- # Gradio UI
41
- gr.Interface(
42
- fn=history_ruler,
43
- inputs=[
44
- gr.Textbox(label="Country / Region", placeholder="e.g., India, Egypt"),
45
- gr.Textbox(label="Year or Time Period", placeholder="e.g., 1526, 18th century")
46
- ],
47
- outputs=gr.Textbox(label="Historical Information"),
48
- title="🏛️ History Ruler Finder"
49
- ).launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%writefile app.py
2
  import gradio as gr
3
  import os
4
  from groq import Groq
5
 
 
6
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
7
 
 
8
  SYSTEM_PROMPT = """
9
  You are a professional history expert AI assistant.
10
 
11
+ Always return:
 
 
 
12
  - Ruler / Authority Name
13
  - Period of Rule
14
  - Region / Country
15
  - Key Contributions (bullet points)
 
 
16
  """
17
 
 
18
  def history_ruler(country, year):
19
  if not country or not year:
20
+ return "Please enter both country and year."
21
 
22
  response = client.chat.completions.create(
23
  model="llama-3.1-8b-instant",
 
27
  ],
28
  temperature=0.2
29
  )
 
30
  return response.choices[0].message.content
31
 
32
+
33
+ # 🔒 FORCE SQUARE OUTPUT USING CSS
34
+ css = """
35
+ #square-box textarea {
36
+ width: 400px !important;
37
+ height: 400px !important;
38
+ resize: none;
39
+ }
40
+ """
41
+
42
+ with gr.Blocks(css=css) as app:
43
+ gr.Markdown("## 🏛️ History Ruler Finder")
44
+
45
+ with gr.Row():
46
+ country = gr.Textbox(label="Country / Region", lines=2)
47
+ year = gr.Textbox(label="Year / Time Period", lines=2)
48
+
49
+ output = gr.Textbox(
50
+ label="Historical Information",
51
+ elem_id="square-box"
52
+ )
53
+
54
+ gr.Button("Find Ruler").click(
55
+ fn=history_ruler,
56
+ inputs=[country, year],
57
+ outputs=output
58
+ )
59
+
60
+ app.launch(share=True)