shivrajkarewar commited on
Commit
229b0f2
·
verified ·
1 Parent(s): a2f1c6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -14
app.py CHANGED
@@ -14,11 +14,21 @@ headers = {"Authorization": f"Bearer {groq_api_key}"}
14
 
15
  # Function to interact with Groq API
16
  def chat_with_groq(user_input):
 
 
 
 
 
 
 
 
 
 
17
  system_prompt = (
18
  "You are an expert materials scientist. When a user asks about the best materials for a specific application, "
19
- "provide the top 3 material choices. For each, include a brief justification with their relevant mechanical, "
20
- "thermal, and chemical properties. First list the key properties required for that application, then present a comparison "
21
- "table of the top 3 materials highlighting those properties. Format the response in markdown."
22
  )
23
 
24
  body = {
@@ -32,19 +42,35 @@ def chat_with_groq(user_input):
32
  response = requests.post(url, headers=headers, json=body)
33
 
34
  if response.status_code == 200:
35
- return response.json()['choices'][0]['message']['content']
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  else:
37
  return f"Error: {response.json()}"
38
 
39
- # Create Gradio interface
40
- interface = gr.Interface(
41
- fn=chat_with_groq,
42
- inputs=gr.Textbox(lines=2, placeholder="Ask about materials for a specific application..."),
43
- outputs=gr.Markdown(),
44
- title="Materials Science Expert Chatbot",
45
- description="Ask about the best materials for a given application. Get the top 3 candidates with detailed comparison of their properties."
46
- )
 
 
 
47
 
48
- # Launch Gradio app
49
  if __name__ == "__main__":
50
- interface.launch()
 
14
 
15
  # Function to interact with Groq API
16
  def chat_with_groq(user_input):
17
+ # Check if question is related to materials science
18
+ keywords = [
19
+ "material", "materials", "alloy", "composite", "polymer", "ceramic",
20
+ "application", "mechanical properties", "thermal properties", "corrosion",
21
+ "creep", "fatigue", "strength", "tensile", "impact", "fracture", "modulus"
22
+ ]
23
+
24
+ if not any(word in user_input.lower() for word in keywords):
25
+ return "⚠️ I am an expert in Materials Science, ask me anything about it and I will try my best to answer. Anything outside, feel free to use ChatGPT! 🙂"
26
+
27
  system_prompt = (
28
  "You are an expert materials scientist. When a user asks about the best materials for a specific application, "
29
+ "provide the top 3 material choices. First, list the key properties required for that application. Then show a clean, "
30
+ "side-by-side comparison in markdown table format of the three materials, with the properties as rows and materials as columns. "
31
+ "Include their relevant mechanical, thermal, and chemical properties. Conclude with a brief summary of which might be best depending on the scenario."
32
  )
33
 
34
  body = {
 
42
  response = requests.post(url, headers=headers, json=body)
43
 
44
  if response.status_code == 200:
45
+ base_response = response.json()['choices'][0]['message']['content']
46
+
47
+ # Append popular related questions for UAE
48
+ popular_questions = (
49
+ "\n\n---\n"
50
+ "#### 🔍 Popular related questions in the UAE:\n"
51
+ "- What are the best corrosion-resistant materials for marine environments (e.g., desalination)?\n"
52
+ "- Which materials are ideal for solar panel coatings and desert heat management?\n"
53
+ "- What materials are used for aerospace structures in extreme climates?\n"
54
+ "- Best high-strength materials for construction in the Gulf region?\n"
55
+ "- What advanced materials are used in electric vehicles and batteries in the UAE?\n"
56
+ )
57
+
58
+ return base_response + popular_questions
59
  else:
60
  return f"Error: {response.json()}"
61
 
62
+ # Build improved layout using Gradio Blocks
63
+ with gr.Blocks(title="Materials Science Expert Chatbot") as demo:
64
+ gr.Markdown("## 🧪 Materials Science Expert\nAsk about the best materials for a specific application.")
65
+
66
+ with gr.Row():
67
+ user_input = gr.Textbox(lines=2, placeholder="e.g. Best materials for high-temperature turbine blades...", label="Ask your question")
68
+ submit_btn = gr.Button("Submit")
69
+
70
+ output = gr.Markdown()
71
+
72
+ submit_btn.click(chat_with_groq, inputs=user_input, outputs=output)
73
 
74
+ # Launch the app
75
  if __name__ == "__main__":
76
+ demo.launch()