Spaces:
Sleeping
Sleeping
Habib U Rehman commited on
Update app.py
Browse files
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 |
-
|
| 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 "
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|